package csl.mq.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Description: TODO 配置延迟消息-定时任务
 */
@Configuration
public class DealyMessageConfig {

    /**
     * 死信队列
     */
    @Bean
    public Queue dealyQueue(){
        HashMap<String,Object> args = new HashMap<>();
        /*将队列修改为延迟队列*/
        //最大存活时间
        args.put("x-message-ttl",10*1000);
        // 消息失活后去到dead-exchange交换机
        args.put("x-dead-letter-exchange","DeadLetter.exc");
        //去到交换机后由该路由key路由他
        args.put("x-dead-letter-routing-key","DeadLetter.key");
        return new Queue("dealy", true, false, false, args);
    }

    /**
     * 死信交换机
     */
    @Bean
    public DirectExchange dealyExchange(){
        return new DirectExchange("DeadLetter.exc");
    }

    /**
     * 绑定队列到交换机
     */
    @Bean
    public Binding bindingQueueToExchange(){
        return BindingBuilder.bind(newQueue()).to(dealyExchange()).with("DeadLetter.key");
    }

    /**
     * 新生队列
     */
    @Bean
    public Queue newQueue(){
        return new Queue("new.queue");
    }
}
